package org.tis.tools.operatelog.module.log.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.tis.tools.model.log.LogData;
import org.tis.tools.model.log.LogDataChange;
import org.tis.tools.model.log.LogOperate;
import org.tis.tools.model.log.vo.LogDataChangeDetail;
import org.tis.tools.model.log.vo.LogDataChangeMessage;
import org.tis.tools.operatelog.module.log.dao.LogDataChangeMapper;
import org.tis.tools.operatelog.module.log.service.ILogDataChangeService;
import org.tis.tools.operatelog.module.log.service.ILogDataService;
import org.tis.tools.operatelog.module.log.service.ILogOperateService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * describe:
 *
 * @author zhaoch
 * @date 2018/3/29
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class LogDataChangeServiceImpl extends ServiceImpl<LogDataChangeMapper, LogDataChange>
        implements ILogDataChangeService {

    @Autowired
    private ILogDataService logDataService;
    @Autowired
    private ILogOperateService logOperateService;

    @Override
    public Page<LogDataChangeDetail> queryPageById(Page<LogDataChange> page, Wrapper<LogDataChange> wrapper) {

        Page<LogDataChangeDetail> dataChangePage = new Page<>();

        //查询数据记录表,找到该具体数据(dataGuid)的所有修改记录
        LogData logData = logDataService.selectById(wrapper.getParamNameValuePairs().get("MPGENVAL1").toString());
        if (null == logData){
            return null;
        }

        //根据数据记录表的数据查询分页信息
        Page<LogData> dataPage = new Page<>();
        dataPage.setCurrent(page.getCurrent());
        dataPage.setSize(page.getSize());
        Wrapper<LogData> dataWrapper = new EntityWrapper<LogData>();
        dataWrapper.eq(LogData.COLUMN_DATA_GUID,logData.getDataGuid());
        Page<LogData> dataPageQue = logDataService.selectPage(dataPage,dataWrapper);
        List<LogData> dataList = dataPageQue.getRecords();
        List<LogDataChangeDetail> changeDetails = new ArrayList<>();
        if (0 != dataList.size()){
            //有数据,则整理数据返回
            for (LogData logDataDetail : dataList){
                if (null != logDataDetail){
                    //查询该数据记录对应的改变数据信息列表
                    Wrapper<LogDataChange> changeWrapper = new EntityWrapper<>();
                    changeWrapper.eq(LogDataChange.COLUMN_GUID_DATA,logDataDetail.getGuid());
                    List<LogDataChange> changes = selectList(changeWrapper);
                    //创建改变数据信息列表的收集对象
                    List<LogDataChangeMessage> logDataChangeMessages = new ArrayList<>();
                    if (0 != changes.size()){
                        for (LogDataChange logDataChange: changes){
                            if (null != logDataChange){
                                LogDataChangeMessage logDataChangeMessage = new LogDataChangeMessage(logDataChange);
                                logDataChangeMessages.add(logDataChangeMessage);
                            }
                        }
                    }
                    //查询每次修改的时间
                    Date operateTime = null;
                    LogOperate logOperate = logOperateService.selectById(logDataDetail.getGuidOperate());
                    if (null != logOperate){
                        operateTime = logOperate.getOperateTime();
                    }
                    //创建待返回信息的对象
                    LogDataChangeDetail logDataChangeDetail = new LogDataChangeDetail(logDataDetail.getGuid(),
                            logDataDetail.getOperateType(), operateTime, logDataChangeMessages);
                    changeDetails.add(logDataChangeDetail);
                }
            }
        }

        //如果查询该页没有数据,返回为空
        dataChangePage.setCurrent(dataPageQue.getCurrent());
        dataChangePage.setSize(dataPageQue.getSize());
        dataChangePage.setTotal(dataPageQue.getTotal());
        dataChangePage.setRecords(changeDetails);

        return  dataChangePage;

//        Page<LogDataChange> pageQue = new Page<>();
//
//        if (null == wrapper.getEntity()){
//            pageQue = selectPage(page);
//        }else {
//
//            LogDataChange logDataChange = wrapper.getEntity();
//
//            Wrapper<LogDataChange> wrapperQue = new EntityWrapper<>();
//            if (null != logDataChange){
//                wrapperQue.eq(LogDataChange.COLUMN_GUID_DATA,logDataChange.getGuidData());
//            }
//            pageQue = selectPage(page,wrapperQue);
//        }
//
//        return pageQue;
    }
}
